package com.jied.duodian.service.Impl;

import com.jied.duodian.dao.DdUserDAO;
import com.jied.duodian.entity.DdUser;
import com.jied.duodian.entity.DdUserExample;
import com.jied.duodian.entity.Dutity.DdOUser;
import com.jied.duodian.service.AuthService;
import com.jied.duodian.util.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Description:
 *
 * @author LiJie
 * @date Created on 2020/10/30
 * WWW https://github.com/j6l
 */
@Service
@Slf4j
@Transactional
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthenticationManager authenticationManager;
    //无法注入解决办法   https://liuyanzhao.com/7852.html


    @Autowired
    private JwtUserDetailsServiceImpl userDetailsService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private DdUserServiceImpl ddUserService;
    @Autowired
    DdUserDAO ddUserDAO;

    /**
     * 添加用户
     *
     * @param userToAdd
     * @return
     */
    @Override
    public Integer register(DdUser userToAdd) {
        if (ddUserService.getUserByName( userToAdd.getUsername() ) != null) {
            return null;
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        final String rawPassword = userToAdd.getPassword();
        userToAdd.setPassword( encoder.encode( rawPassword ) );
        userToAdd.setCreateTime( new Date() );
        userToAdd.setStatus( 1 );
        userToAdd.setRole( 3 );
        return ddUserService.addUserList( userToAdd );
    }

    @Override
    public String login(String username, String password) {

        UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken( username, password );
        final Authentication authentication = authenticationManager.authenticate( upToken );
        SecurityContextHolder.getContext().setAuthentication( authentication );
        final UserDetails userDetails = userDetailsService.loadUserByUsername( username );
        final String token = jwtTokenUtil.generateToken( userDetails );
        return token;
    }

    @Override
    public String refresh(String oldToken) {
        final String token = oldToken.substring( jwtTokenUtil.getTokenHead().length() );
        String username = jwtTokenUtil.getUsernameFromToken( token );
        DdOUser user = (DdOUser) userDetailsService.loadUserByUsername( username );
        if (jwtTokenUtil.isTokenExpired( token )) {
            return jwtTokenUtil.refreshToken( token );
        }
        return null;
    }
}
